#include "logMgr.h"

static LogMgr logMgr;

void logInit()
{
     auto console = spdlog::stdout_color_mt("console");
     console->set_level(spdlog::level::info);
     console->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%^%L%$] [%t] %v [%s:%#]");

    // auto rotating_logger =
    //     spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1024 * 1024 * 5, 10);//5mb, 10file
    // auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);

    spdlog::init_thread_pool(32768, 2); // queue with max 32k items 2 backing thread.
    auto async_file = spdlog::daily_logger_mt< spdlog::async_factory_impl<spdlog::async_overflow_policy::discard_new> >  // 队列满时,丢掉新的msg
    ("async_file_logger", "logs/async_log.txt", 4, 0, false, 30); //a new file is created every day on 4:00am and 30 rotated files.

    async_file->set_level(spdlog::level::trace);
    async_file->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%^%L%$] [%t] %v [%s:%#]");

    // // auto async_file =
    // // spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger",
    // // "logs/async_log.txt");

    // auto async_logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>(
    //     "async_logger",
    //     "logs/async.log",
    //     spdlog::thread_pool(),
    //     spdlog::async_overflow_policy::discard_new // 队列满时,丢掉新的msg
    // );


    // logger.trace("multi_sink_example 0");
    // logger.debug("multi_sink_example 1");
    // logger.info("multi_sink_example 2");
    // logger.warn("multi_sink_example 3");
    // logger.error("multi_sink_example 4");
    // logger.critical("multi_sink_example 5");

    //spdlog::set_default_logger(logger);


    // auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
    // auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("mylog.txt", 1024*1024*10, 3);
    // std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};
    // auto logger = std::make_shared<spdlog::async_logger>("loggername", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
    // spdlog::register_logger(logger);
}

LogMgr::LogMgr()
{
    try {

        logInit();

        //LOG_INFO("this is my macro");
        //LOG_ERROR("this is my macro");

        // Flush all *registered* loggers using a worker thread every 3 seconds.
        // note: registered loggers *must* be thread safe for this to work correctly!
        spdlog::flush_every(std::chrono::seconds(3));

        // Apply some function on all registered loggers
        //spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example. This message is from spdlog::apply_all()"); });
    }

    // Exceptions will only be thrown upon failed logger or sink construction (not during logging).
    catch (const spdlog::spdlog_ex &ex) {
        std::printf("Log initialization failed: %s\n", ex.what());
        return;
    }
}

LogMgr::~LogMgr()
{
    // Release all spdlog resources, and drop all loggers in the registry.
    // This is optional (only mandatory if using windows + async log).
    spdlog::shutdown();
}

